import { loginByUsername, logout, getUserInfo, loginCompany, getCode } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { switchTemperature, getTemperature } from '@/api/index/index'

const user = {
  state: {
    baseUrl: `http://rap2.taobao.org:38080/app/mock/260114/`,
    is_centigrade: '',
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    },
    adminId: '',
    merchantId: '',
    nickname: ''
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ADMINID: (state, adminId) => {
      state.adminId = adminId
    },
    SET_MERCHANTID: (state, merchantId) => {
      state.merchantId = merchantId
    },
    SET_NICKNAME: (state, nickname) => {
      state.nickname = nickname
    },
    SET_IS_CENTIGRADE: (state, is_centigrade) => {
      state.is_centigrade = is_centigrade
    }

  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        loginByUsername(username, userInfo.password).then(response => {
          console.log(response)
          if (response.data.status) {
            const data = response.data
            commit('SET_TOKEN', data.token)
            setToken(response.data.token)
            resolve()
          } else {
            reject(response.data.msg)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetCode({ commit }, data) {
      return new Promise((resolve, reject) => {
        getCode(data.mobile, data.code_type).then(response => {
          resolve(response.data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    LoginCompany({ commit }, data) {
      return new Promise((resolve, reject) => {
        loginCompany(data.username, data.password, data.code, data.merchant_id).then(response => {
          commit('SET_TOKEN', response.data.token)
          setToken(response.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token).then(response => {
          // 由于mockjs 不支持自定义状态码只能这样hack
          console.log('getUserInfo')
          console.log(response)
          if (!response.data) {
            reject('Verification failed, please login again.')
          }
          const data = response.data

          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
          } else {
            reject('getInfo: roles must be a non-null array!')
          }
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.data.head_img)
          commit('SET_INTRODUCTION', data.introduction)
          commit('SET_ADMINID', data.data.id)
          commit('SET_MERCHANTID', data.data.merchant_id)
          commit('SET_NICKNAME', data.data.nickname)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    },

    // 获取温度信息
    GetTemperature({ commit, state }) {
      return new Promise((resolve, reject) => {
        getTemperature(state.token, { id: 1 }).then(response => {
          const data = response.data
          if (data.status) {
            commit('SET_IS_CENTIGRADE', data.data.is_centigrade)
          } else {
            reject('获取温度信息失败')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 修改温度信息
    SwitchTemperature({ commit, state }, is_centigrade) {
      return new Promise((resolve, reject) => {
        switchTemperature(state.token, { id: 1, is_centigrade: is_centigrade }).then(response => {
          const data = response.data
          if (data.status) {
            commit('SET_IS_CENTIGRADE', is_centigrade)
          } else {
            reject('修改温度信息失败')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
